package com.lcy.routine.case1.client;

import com.lcy.routine.case1.request.GoodsDetailRequest;
import com.lcy.routine.case1.request.GoodsListRequest;
import com.lcy.routine.case1.response.GoodsDetailResponse;
import com.lcy.routine.case1.response.GoodsListResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class jdClient {

    private String postRequest(String param){
        return HttpUtil.createPost("https://api.m.jd.com/api").body(param).execute().body();
    }

    /**
     * 商品列表接口
     */
    public GoodsListResponse goodsList(GoodsListRequest request){
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, GoodsListResponse.class);
    }

    /**
     * 商品详情接口
     */
    public GoodsDetailResponse goodsDetail(GoodsDetailRequest request){
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, GoodsDetailResponse.class);
    }

    public static void main(String[] args) {
        jdClient client = new jdClient();
        GoodsListRequest request = new GoodsListRequest();
        GoodsListResponse goodsListResponse = jdClient.goodsList(request);
        System.out.println(goodsListResponse);

        GoodsDetailRequest detailRequest = new GoodsDetailRequest();
        GoodsDetailResponse goodsDetailResponse = jdClient.goodsDetail(detailRequest);
        System.out.println(goodsDetailResponse);
    }

}
